﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using EHTSaleManager.Code.BLL;
using EHTSaleManager.Code.Entity;
using EHTSaleManager.Code.Utility;
using System.Data;
using System.IO;
using System.Data.OleDb;
using Excel = Microsoft.Office.Interop.Excel;


namespace EHTSaleManager.UIAdmin
{
    public partial class AdminProductImportData : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        private Boolean ValidateData()
        {
            String message = "";
            Boolean isSuccess = true;
            string sExtendsion = ".xls|.XLS|.csv|.CSV";

            try
            {
                if (this.fufile.HasFile && sExtendsion.IndexOf(Path.GetExtension(fufile.PostedFile.FileName)) < 0)
                {
                    isSuccess = false;
                    message = "Last opp fil CSV eller XSL.";
                }
            }
            catch (Exception ex)
            {
                isSuccess = false;
            }
            if (!isSuccess)
                ltrMessage.Text = message;

            return isSuccess;
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("AdminProducts.aspx");
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (!ValidInput())
                return;
            // kiem tra xem file co ton tai tren server ko
            // neu ton tai thi xoa
            // Start
            //try
            //{
                string filename = fufile.FileName.ToString();
                try
                {
                    FileInfo file = new FileInfo(Server.MapPath("~\\ExcelTem\\Excel\\" + filename));
                    if (file.Exists)
                    {
                        File.Delete(Server.MapPath("~/ExcelTem/Excel/") + filename);
                    }                    
                }
                catch (Exception ex)
                {
                    ltrMessage.Text = "Erorr";
                    return ;
                }
                // End

                // upload file excel len server
                //Start                              
                string str = Server.MapPath("~/");
                str += "ExcelTem\\Excel\\";
                DirectoryInfo directory = new DirectoryInfo(str);

                if (!directory.Exists)
                {
                    DirectoryInfo drt = new DirectoryInfo("\\ExcelTem");
                    if (!drt.Exists)
                    {
                        directory = new DirectoryInfo(Server.MapPath("~/"));

                        directory = directory.CreateSubdirectory("ExcelTem");

                    }
                    directory = new DirectoryInfo(Server.MapPath("~/").ToString() + "\\ExcelTem");
                    directory = directory.CreateSubdirectory("Excel");
                }
                fufile.SaveAs(Server.MapPath("~/") + "ExcelTem//Excel//" + filename);
                string pathfile = Server.MapPath("~/") + "ExcelTem//Excel//" + filename;
                // end


                try
                {
                    Excel.Application xlApp = new Excel.Application();
                    Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(pathfile);
                    Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[1];
                    Excel.Range xlRange = xlWorksheet.UsedRange;

                    int rowCount = xlRange.Rows.Count;
                    for (int i = 4; i <= rowCount; i++)
                    {
                        Product theproduct = new Product();

                        string id = xlRange.Cells[i, 1].Value2.ToString();



                        theproduct.CategoryID = Int32.Parse(xlRange.Cells[i, 1].Value2.ToString());
                        theproduct.ProductID = Int32.Parse(xlRange.Cells[i, 3].Value2.ToString());
                        theproduct.ProductName = xlRange.Cells[i, 4].Value2.ToString();
                        theproduct.QuantityPerUnit = xlRange.Cells[i, 5].Value2.ToString();
                        theproduct.Price = float.Parse(xlRange.Cells[i, 6].Value2.ToString());
                        theproduct.PricePerUnit = float.Parse(xlRange.Cells[i, 7].Value2.ToString());
                        theproduct.UnitsInStock = xlRange.Cells[i, 8].Value2.ToString();
                        theproduct.Discount = float.Parse(xlRange.Cells[i, 9].Value2.ToString());
                        theproduct.SupplierID = -1;

                        DataSet dsproduct = ProductProvider.CheckExist(xlRange.Cells[i, 4].Value2.ToString());
                        if (dsproduct.Tables[0].Rows.Count > 0)
                        {
                            ProductProvider.ImportData(theproduct, false);
                        }
                        else
                        {
                            ProductProvider.ImportData(theproduct, true);
                        }
                    }
                    xlApp.Workbooks.Close();



                    Response.Redirect("AdminProducts.aspx");
                }
                catch (Exception ex)
                {
                    ltrMessage.Text = "Erorr";
                    return;
                }
           
        }
 
        // kiem tra file co hay ko
        private bool ValidInput()
        {
            if (fufile.FileName.ToString() == "")
            {
                ltrMessage.Text = "File is not null";
                return false;
            }
            return true;
        }
       
    }
}